package com.alpha.eceasy.mall.util;

import com.alpha.eceasy.extension.BaseFormInfo;
import com.alpha.eceasy.extension.BaseService;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.extension.BaseSqlMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class MybatisGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://10.193.151.29:26345/eceasy_hw_main?serverTimezone=GMT%2b8",
                        "eceasy_hw_main_root", "VT3ca67Ey8hPWevRWpc#utc7")
                .globalConfig(builder -> {
                    builder.author("huangpengfei") // 设置作者
                            .enableSpringdoc() // 开启 springdoc 模式
                            .outputDir("C:\\Users\\huangpf\\Downloads"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.alpha.eceasy.mall") // 设置父包名
                            .entity("api.entity")
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, null)) // 设置mapperXml生成路径
                    ;
                })
                .strategyConfig(builder -> builder
                        .addInclude("info_product") // 设置需要生成的表名
                        .addTablePrefix("sys_", "info_", "scms_") // 设置过滤表前缀
                        .entityBuilder().enableFileOverride().superClass(BaseFormInfo.class).enableLombok().logicDeleteColumnName("deleted")
                        .mapperBuilder().enableFileOverride().superClass(BaseSqlMapper.class)
                        .serviceBuilder().enableFileOverride().convertServiceFileName(entityName -> entityName + ConstVal.SERVICE).superServiceClass(BaseService.class).superServiceImplClass(BaseServiceImpl.class)
                        .controllerBuilder().enableFileOverride().enableRestStyle().enableHyphenStyle()
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
